package com.work.lxykeeper.module.homepage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Base64;
import android.view.View;

import com.work.lxykeeper.R;
import com.work.lxykeeper.base.BaseActivity;
import com.work.lxykeeper.bean.CapetureDetail;
import com.work.lxykeeper.cache.Ckey;
import com.work.lxykeeper.cache.SPutils;
import com.work.lxykeeper.databinding.ActivityCaptureResultBinding;
import com.work.lxykeeper.httpretrofit.MyCallback;
import com.work.lxykeeper.httpretrofit.RetrofitManager;
import com.work.lxykeeper.utils.AESUtils;
import com.work.lxykeeper.utils.PersonLableUtil;

import retrofit2.Call;

/**
 * Created by WZ on 2020/12/1.
 * description:扫码结果页面
 */
public class CaptureResultActivity extends BaseActivity {
    private String result;
    private ActivityCaptureResultBinding binding;


    @Override
    protected View getLayoutView() {
        binding = ActivityCaptureResultBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("详情");
        result = getIntent().getStringExtra("result");
        getHttpDetail(result);
    }

    //、app-手机扫码预约信息查询
    private void getHttpDetail(String result) {
        Call<CapetureDetail> call = RetrofitManager.getInstance().codeDetail(result);
        call.enqueue(new MyCallback<CapetureDetail>() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onSuccess(CapetureDetail capetureDetail) {
                CapetureDetail.DataBean detail = capetureDetail.getData();
                if (detail.getCurrentstatus() == 1) { //1、正常通行（绿色显示），2、未到预约日期（红色显示），3、已核销（红色显示），在头部显示,4、无效
                    binding.ltGobg.setBackgroundResource(R.drawable.bg1_oval);
                    binding.ivGoicon.setImageResource(R.mipmap.zhengchang);
                } else if (detail.getCurrentstatus() == 2) {
                    binding.ltGobg.setBackgroundResource(R.drawable.bg2_oval);
                    binding.ivGoicon.setImageResource(R.mipmap.weidaoshijian);
                } else if (detail.getCurrentstatus() == 3) {
                    binding.ltGobg.setBackgroundResource(R.drawable.bg2_oval);
                    binding.ivGoicon.setImageResource(R.mipmap.yihexiao);
                } else if (detail.getCurrentstatus() == 4) {
                    binding.ltGobg.setBackgroundResource(R.drawable.bg3_oval);
                    binding.ivGoicon.setImageResource(R.mipmap.yihexiao);
                }
                binding.tvLable.setText(PersonLableUtil.getCurrentStutas(detail.getCurrentstatus()));//预约状态
                binding.tvName.setText(detail.getPersonname());//预约人姓名
                try {
                    binding.tvName2.setText(AESUtils.Decrypt(detail.getCardnumber(), SPutils.get(Ckey.AESKEY)));//预约人身份证号
                } catch (Exception e) {
                    e.printStackTrace();
                }
                binding.tvName3.setText(detail.getTelphone());//预约人手机号
                binding.tvName4.setText(PersonLableUtil.getPersonType(detail.getType()));//预约类型：1个人，2团体，3活动4工作人员预约',
                binding.tvName5.setText(detail.getTeamname() + "");//预约单位名称
                if (detail.getAdate() != null) {
                    binding.tvName6.setText(detail.getAdate().split(" ")[0] + "");//预约时间
                }
                binding.tvName7.setText(detail.getStarttime() + "");//预约开始时间
                binding.tvName8.setText(detail.getEndtime() + "");//预约结束时间
                binding.tvName9.setText(detail.getPersonnum() + "");//预约人数
//                GlideImgManager.glideLoaderBitmap(CaptureResultActivity.this, stringtoBitmap(detail.getFacedata()), ivHead, 1);//预约人脸照片
                binding.ivHead.setImageBitmap(stringtoBitmap(detail.getFacepath()));
                binding.tvName10.setText(PersonLableUtil.getPwStatus(detail.getStatus()));//票务状态
                binding.tvName11.setText(PersonLableUtil.getCreateType(detail.getCreatetype()));//预约方式
                binding.tvName12.setText(detail.getActivityname());//活动名称
                binding.tvName13.setText(detail.getActivitystime());//活动开始时间
                binding.tvName14.setText(detail.getActivityetime());//活动结束时间
                binding.tvName15.setText(detail.getUsetime());//使用时间
                binding.tvName16.setText(detail.getCanceltime());//取消时间
                binding.tvName17.setText(PersonLableUtil.getPeopleLable(detail.getIsappointment()));//预约人标识
                binding.tvName18.setText(detail.getEmployeename());//工作人员姓名
                binding.tvName19.setText("");//同行人员姓名
                binding.tvName20.setText("");//同行人身份证号

            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void initData() {

    }

    public Bitmap stringtoBitmap(String string) {
        //将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
